<template>
  <a-spin :spinning="spinning != 0">
    <a-form :form="form"
            layout="horizontal"
            :hideRequiredMark="false"
            style="">
      <a-row class="grid-row">
        <a-col v-bind="colLayout2.cols" class="grid-col">
          <a-form-item :label="$t('m.system.sqlconfig.entity.sqlName')" :label-col="colLayout2.labelCol" :wrapper-col="colLayout2.wrapperCol">
            <a-input
              :disabled="disabledRules.sqlName"
              placeholder=""
              type="text"
              v-decorator="['sqlName', validatorRules.sqlName]"
            />
          </a-form-item>
        </a-col>
        <a-col v-bind="colLayout2.cols" class="grid-col">
          <a-form-item :label="$t('m.system.sqlconfig.entity.sqlCode')" :label-col="colLayout2.labelCol" :wrapper-col="colLayout2.wrapperCol">
            <a-input
              :disabled="disabledRules.sqlCode"
              placeholder=""
              type="text"
              v-decorator="['sqlCode', validatorRules.sqlCode]"
            />
          </a-form-item>
        </a-col>
        <a-col v-bind="colLayout2.fixed.cols" class="grid-col">
          <a-form-item label="SQL" :label-col="colLayout2.fixed.labelCol" :wrapper-col="colLayout2.fixed.wrapperCol">
            <a-textarea
              :autoSize="{
                    minRows: 4,
                    maxRows: 6
                    }"
              :disabled="disabledRules.sqlQuery"
              :placeholder="$t('m.common.tip.sql_warn')"
              :rows="4"
              v-decorator="['sqlQuery', validatorRules.sqlQuery]"
            />
          </a-form-item>
        </a-col>
        <!--<a-col v-bind="colLayout2.cols" class="grid-col">
            <a-form-item label="所属应用，为空表示公共，预留扩展应用内配置用" :label-col="colLayout2.labelCol" :wrapper-col="colLayout2.wrapperCol">
    <a-input
            :disabled="disabledRules.groupId"
            placeholder="请输入所属应用，为空表示公共，预留扩展应用内配置用"
            type="text"
    v-decorator="['groupId', validatorRules.groupId]"
    />
            </a-form-item>
        </a-col>
        <a-col v-bind="colLayout2.cols" class="grid-col">
            <a-form-item label="入参，预留扩展用" :label-col="colLayout2.labelCol" :wrapper-col="colLayout2.wrapperCol">
    <a-input
            :disabled="disabledRules.inParameter"
            placeholder="请输入入参，预留扩展用"
            type="text"
    v-decorator="['inParameter', validatorRules.inParameter]"
    />
            </a-form-item>
        </a-col>-->
        <a-col v-bind="colLayout2.cols" class="grid-col">
          <a-form-item label="Key" :label-col="colLayout2.labelCol" :wrapper-col="colLayout2.wrapperCol">
            <a-input
              :disabled="disabledRules.sqlKey"
              placeholder=""
              type="text"
              v-decorator="['sqlKey', validatorRules.sqlKey]"
            />
          </a-form-item>
        </a-col>
        <a-col v-bind="colLayout2.cols" class="grid-col">
          <a-form-item label="Text" :label-col="colLayout2.labelCol" :wrapper-col="colLayout2.wrapperCol">
            <a-input
              :disabled="disabledRules.sqlText"
              placeholder=""
              type="text"
              v-decorator="['sqlText', validatorRules.sqlText]"
            />
          </a-form-item>
        </a-col>
        <a-col v-bind="colLayout2.cols" class="grid-col">
          <a-form-item :label="$t('m.eform.apptable.entity.dsName')" :label-col="colLayout2.labelCol" :wrapper-col="colLayout2.wrapperCol">
            <select-ds v-decorator="[ 'dsCode', validatorRules.dsCode]" placeholder=""
                       :allow-clear="false"></select-ds>
          </a-form-item>
        </a-col>
        <a-col v-bind="colLayout2.cols" class="grid-col">
          <a-form-item :label="$t('m.common.label.sort')" :label-col="colLayout2.labelCol" :wrapper-col="colLayout2.wrapperCol">
            <a-input-number
              style="width: 100%"
              :min="0"
              :max="999999999"
              :disabled="disabledRules.orderBy"
              :step="1"
              placeholder=""
              v-decorator="['orderBy', validatorRules.orderBy]"
            />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-spin>
</template>

<script>

import {FormMixin} from '@/mixins/FormMixin'
import {GardLayoutMixin} from '@/mixins/GardLayoutMixin'
import {YchCodeEditor} from '@yuncheng/ych-components'
import SelectDs from '../select/tag/SelectDs'
import YchConstant from "@/api/YchConstant";
import {duplicateCheck} from '@/api/api'

export default {
  name: "ActAppSqlFormModal",
  mixins: [FormMixin, GardLayoutMixin],
  components: {
    YchCodeEditor,
    SelectDs
  },
  data() {
    return {
      validatorRules: {
        sqlName: {
          rules: [
            {required: true, message: this.$t('m.system.sqlconfig.verify.sqlName')},
            {validator: this.validateSqlName}
          ]
        },
        sqlCode: {
          rules: [
            {required: true, message: this.$t('m.system.sqlconfig.verify.sqlCode')},
            {validator: this.validateSqlCode}
          ]
        },
        sqlQuery: {
          rules: [{required: true, message: this.$t('m.common.tip.sql_warn')},]
        },
        groupId: {
          rules: []
        },
        inParameter: {
          rules: []
        },
        sqlKey: {
          rules: [{required: true, message: this.$t('m.system.sqlconfig.verify.sqlKey')},]
        },
        sqlText: {
          rules: [{required: true, message: this.$t('m.system.sqlconfig.verify.sqlText')},]
        },
        dsCode: {
          initialValue: 'dataSourceDefault',
          rules: [{required: true, message:this.$t('m.eform.appview.verify.dsCode')},]
        },
        orderBy: {
          rules: [{required: true, message: this.$t('m.system.sqlconfig.verify.orderBy')},]
        },
      },
      disabledRules: {
        sqlName: false,
        sqlCode: false,
        sqlQuery: false,
        groupId: false,
        inParameter: false,
        sqlKey: false,
        sqlText: false,
        dsCode: false,
        orderBy: false,
      },
      url: {
        queryById: YchConstant.PREFIX_EFORM + '/manage/actAppSql/queryById',
        add: YchConstant.PREFIX_EFORM + "/manage/actAppSql/add",
        edit: YchConstant.PREFIX_EFORM + "/manage/actAppSql/edit",
      },
    }
  },
  methods: {
    validateSqlCode(rule, value, callback) {
      let reg = new RegExp("[\\u4E00-\\u9FFF]+", "g");
      if (reg.test(value)) {
        callback(this.$t('m.common.tip.code_format'))
        return
      }
      var params = {
        tableName: 'act_app_sql',
        fieldName: 'sql_code',
        fieldVal: value,
        dataId: this.model.id
      }
      duplicateCheck(params).then((res) => {
        if (res.success) {
          callback()
        } else {
          callback(this.$t('m.system.sqlconfig.verify.sqlCode_repeat'))
        }
      })
    },
    validateSqlName(rule, value, callback) {
      var params = {
        tableName: 'act_app_sql',
        fieldName: 'sql_name',
        fieldVal: value,
        dataId: this.model.id
      }
      duplicateCheck(params).then((res) => {
        if (res.success) {
          callback()
        } else {
          callback(this.$t('m.system.sqlconfig.verify.sqlName_repeat'))
        }
      })
    },
  },
}
</script>
